//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef SOCKETBASE_H
#define SOCKETBASE_H

#include <QObject>
#include <QObject>
#include <QTcpSocket>
#include <QString>
#include <QByteArray>
#include <QLabel>
#include <QTextEdit>
#include <QTime>


/*

*/
class SocketBase : public QObject
{
    Q_OBJECT
protected:
    QTcpSocket *socket;
    QString host;
    quint16 port;
    QLabel *stateLabel;
    QTextEdit *logTextEdit;

public:
    SocketBase(QObject *parent = 0);
    SocketBase(QString host, quint16 port, QObject *parent = 0);

    void setHost(QString host);
    void setPort(quint16 port);

    /* returns a host */
    QString getHost();

    /* returns a port */
    quint16 getPort();

    /* returns host:port */
    virtual QString toString();

    /* reconnects to a host */
    void connectToHost();

    /* logTextEdit (if set) is appended everytime when a new error occures*/
    void setLogTextEdit(QTextEdit *logTextEdit);
    void setStateLabel(QLabel *stateLabel);

    static quint16 _htons(quint16 x);
    static quint16 _ntohs(quint16 x);
    static quint32 _htonl(quint32 x);
    static quint32 _ntohl(quint32 x);
private slots:
    void displayState(QAbstractSocket::SocketState state);
    void displayError(QAbstractSocket::SocketError error);
};

#endif // SOCKETBASE_H
